1620E - Replace the Numbers - CodeForces Solution


constructive algorithms data structures dsu implementation *1900

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#define ll long long
#define endl "\n"
#define fori(n)         for (ll i=0; i<n; i++) 
#define forj(n)         for (ll j=0; j<n; j++) 
#define fork(n)         for (ll k=0; k<n; k++) 
#define Sort(a)         sort(a.begin(),a.end())
#define pd pair<ll, ll>
#define  Alice  cout<<"Alice"<<endl
#define Bob  cout<<"Bob"<<endl

using namespace std;

// ncr 

/*  void prllNcR(ll n, ll r)
{
 
    // p holds the value of n*(n-1)*(n-2)...,
    // k holds the value of r*(r-1)...
    long long p = 1, k = 1;
 
    // C(n, r) == C(n, n-r),
    // choosing the smaller value
    if (n - r < r)
        r = n - r;
 
    if (r != 0) {
        while (r) {
            p *= n;
            k *= r;
 
            // gcd of p, k
            long long m = __gcd(p, k);
 
            // dividing by gcd, to simplify
            // product division by their gcd
            // saves from the overflow
            p /= m;
            k /= m;
 
            n--;
            r--;
        }
 
        // k should be simplified to 1
        // as C(n, r) is a natural number
        // (denominator should be 1 ) .
    }
 
    else
        p = 1;
 
    // if our approach is correct p = ans and k =1
    cout << p << endl;
}  */







//nCr%p
/*     unsigned long long power(unsigned long long x, 
                                  ll y, ll p)
{
    unsigned long long res = 1; // Initialize result
  
    x = x % p; // Update x if it is more than or
    // equal to p
  
    while (y > 0) 
    {
      
        // If y is odd, multiply x with result
        if (y & 1)
            res = (res * x) % p;
  
        // y must be even now
        y = y >> 1; // y = y/2
        x = (x * x) % p;
    }
    return res;
}
  
// Returns n^(-1) mod p
unsigned long long modInverse(unsigned long long n,  
                                            ll p)
{
    return power(n, p - 2, p);
} 
  
// Returns nCr % p using Fermat's little
// theorem.
 unsigned long long nCrModPFermat(unsigned long long n,
                                 ll r, ll p)
{
    // If n<r, then nCr should return 0
    if (n < r)
        return 0;
    // Base case
    if (r == 0)
        return 1;
  
    // Fill factorial array so that we
    // can find all factorial of r, n
    // and n-r
    unsigned long long fac[n + 1];
    fac[0] = 1;
    for (ll i = 1; i <= n; i++)
        fac[i] = (fac[i - 1] * i) % p;
  
    return (fac[n] * modInverse(fac[r], p) % p
            * modInverse(fac[n - r], p) % p)
           % p;
}    





ll M=998244353; */
// GRAPH
/*  vector<vector<ll>>graph(100001);
vector<ll>visited(100001,0);
vector<ll>counti(100001,0);

ll dfs(ll n)
{

    if(!visited[n])
    {
        ll count=0;
        if(graph[n].size()==1 && n!=1)
        count++;
            visited[n]=1;

                for(auto child:graph[n])
                count=count+dfs(child);
                counti[n]=count;
                return count;
 
    }
    else
    return 0;
}     */
 






// SIEVE 
/*      const ll N = 10002;
vector<ll> lp(N+1);
vector<ll> pr;

void Sieve()
{
for (ll i=2; i <= N; ++i) {
    if (lp[i] == 0) {
        lp[i] = i;
        pr.push_back(i);
    }
    for (ll j = 0; i * pr[j] <= N; ++j) {
        lp[i * pr[j]] = pr[j];
        if (pr[j] == lp[i]) {
            break;
        }
    }
} 
}      */



// BINPOW
/* ll power(ll x, ll y, ll p)
{
    ll res = 1;     // Initialize result
 
    x = x % p; // Update x if it is more than or
                // equal to p
  
    if (x == 0) return 0; // In case x is divisible by p;
 
    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y & 1)
            res = (res*x) % p;
 
        // y must be even now
        y = y>>1; // y = y/2
        x = (x*x) % p;
    }
    return res;
} */
/*  vector<long long> trial_division2(long long n) {
    vector<long long> factorization;
    while (n % 2 == 0) {
        factorization.push_back(2);
        n /= 2;
    }
    for (long long d = 3; d * d <= n; d += 2) {
        while (n % d == 0) {
            factorization.push_back(d);
            n /= d;
        }
    }
    if (n > 1)
        factorization.push_back(n);
    return factorization;
}  */



int main()
{
    ios::sync_with_stdio(false);
	cin.tie(nullptr); 
    ll t,i;
    t=1;
    //cin>>t; 
    fori(t)
    {
        
        vector<ll>v(500002);
        forj(500002)
        {
            v[j]=j;
        }
        ll n;
        cin>>n;
        vector<pair<ll,vector<ll>>>vx;
        forj(n)
        {
            ll x;
            cin>>x;
            vector<ll>vy;
            fork(x)
            {
                ll y;
                cin>>y;
                vy.push_back(y);
            }
            vx.push_back({x,vy});
        }
        vector<ll>ans;
        for(ll j=n-1;j>=0;j--)
        {
            if(vx[j].first==2)
            {
                v[vx[j].second[0]]=v[vx[j].second[1]];

            }
            else
            {
                ans.push_back(v[vx[j].second[0]]);

            }

        }
        forj(ans.size())
        cout<<ans[ans.size()-1-j]<<" ";
        cout<<endl;


    }
    
}


Comments

Submit
0 Comments
More Questions

1593C - Save More Mice
1217. Minimum Cost to Move Chips to The Same Position
347. Top K Frequent Elements
1503. Last Moment Before All Ants Fall Out of a Plank
430. Flatten a Multilevel Doubly Linked List
1290. Convert Binary Number in a Linked List to Integer
1525. Number of Good Ways to Split a String
72. Edit Distance
563. Binary Tree Tilt
1306. Jump Game III
236. Lowest Common Ancestor of a Binary Tree
790. Domino and Tromino Tiling
878. Nth Magical Number
2099. Find Subsequence of Length K With the Largest Sum
1608A - Find Array
416. Partition Equal Subset Sum
1446. Consecutive Characters
1618A - Polycarp and Sums of Subsequences
1618B - Missing Bigram
938. Range Sum of BST
147. Insertion Sort List
310. Minimum Height Trees
2110. Number of Smooth Descent Periods of a Stock
2109. Adding Spaces to a String
2108. Find First Palindromic String in the Array
394. Decode String
902. Numbers At Most N Given Digit Set
221. Maximal Square
1200. Minimum Absolute Difference
1619B - Squares and Cubes